
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Map;

import edu.model.Usuario;

public class LoginSession extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init(ServletConfig conf) throws ServletException{
		super.init(conf);
	}
	
	public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		res.setContentType("text/html");

		String usu = null;
		String pass = null;
		Double dusu = null;
		
		/*
		PrintWriter out = res.getWriter();
		*/
		
		Usuario oUsu = new Usuario();
		 
		String sRet = "success";
		/*Firmado oFirm;
		
		oUsu.setId(cve);
		oUsu.setContrasena(pwd);
		*/
		
		try{
			usu = (String) req.getParameter("usuario");
			pass = (String) req.getParameter("password");
			dusu = Double.valueOf(usu).doubleValue();
			
			oUsu.setId(dusu);
			oUsu.setContrasena(pass);
			//if (oUsu.buscarCvePwd()){
			if(oUsu.buscarCvePwd()){
				//oFirm = new Firmado();
				//oFirm.setUsuario(oUsu);
				//HttpSession session = (HttpSession)facesContext.getExternalContext().getSession(false);
				HttpSession session = req.getSession(true);
				/*
				session.setAttribute("tipo", oUsu.getTipo());
				session.setAttribute("firmado", "Si");
				
				req.setAttribute("tipo", oUsu.getTipo());
				req.setAttribute("firmado", "Si");
				*/
				((Map)session.getAttribute("myApp")).put("tipo", oUsu.getTipo());
				((Map)session.getAttribute("myApp")).put("firmado", "Si");
				//((Map)application.getAttribute("myApp")).put("myVariable", "test.");
				
				sRet = "success - usu: " + usu + " pass : " + pass + " tipo" + oUsu.getTipo();
				

				
			}else{
				sRet = "faild - usu: " + usu + " pass : " + pass;
			}
		}catch(Exception e){
			sRet = "error - usu: " + usu + " pass : " + pass;
			e.printStackTrace();
		}
/*
		PrintWriter out = res.getWriter();
		out.println(sRet);
		*/
		res.sendRedirect("index.cfm");
	}
	
	public static void main(String arg[]) throws Exception{
		Usuario usu = new Usuario();
		usu.setId(1.0);
		usu.setContrasena("hugo");
		System.out.print(usu.getContrasena());
	
	}
	

}